#include <iostream>
#include <cstdlib>
/* 
逆序输入n个元素创建单链表
 */
typedef struct lnode
{
    int data;
    struct lnode *next;
} lnode, *linklist;

int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
// 头插法,插入逆序
void headinsert(linklist &l)
{
    lnode *head, *s;
    head = (lnode *)malloc(sizeof(lnode));
    head->next = nullptr;
    for (int i = 0; i < sizeof(a) / sizeof(int); i++)
    {
        s = (lnode *)malloc(sizeof(lnode));
        s->data = a[i];
        s->next = head->next;
        head->next = s;
    }
    l = head;
}

// 尾插法，插入正序
void rearinsert(linklist &l)
{
    l = (lnode *)malloc(sizeof(lnode));
    l->next = nullptr;
    lnode *r = l, *s;
    for (int i = 0; i < sizeof(a) / sizeof(int); i++)
    {
        s = (lnode *)malloc(sizeof(lnode));
        s->data = a[i];
        s->next = nullptr;
        r->next = s;
        r = s;
    }
    r = nullptr;
}

int main(int argc, char const *argv[])
{
    linklist l, ll;
    headinsert(l);
    rearinsert(ll);
    return 0;
}
